package com.yyjj.yyjj.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyjj.yyjj.acl.service.RoleService;
import com.yyjj.yyjj.common.result.Result;
import com.yyjj.yyjj.model.acl.Role;
import com.yyjj.yyjj.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "角色接口")
@RequestMapping("/admin/acl/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    //角色条件分页查询
    @ApiOperation("角色列表")
    @GetMapping("/{current}/{limit}")
    public Result pageList(@PathVariable("current") Long current,
                           @PathVariable("limit") Long limit,
                           RoleQueryVo roleQueryVo) {
        //创建一个page对象，传递当前页和每页记录数
        //current 当前页
        //limit 每页记录数
        Page<Role> pageParam = new Page<>(current, limit);
        //调用service方法实现条件分页查询，返回分页对象
        IPage<Role> pageModel = roleService.selectRolePage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }

    //根据id查询角色
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    //添加角色
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        roleService.save(role);
        return Result.ok(null);
    }

    //修改角色
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.ok(null);
    }

    //根据id删除角色
    @ApiOperation("根据id删除角色")
    @DeleteMapping("/remove/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        roleService.removeById(id);
        return Result.ok(null);
    }

    //批量删除角色
    @ApiOperation("批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){ //前端传的是json，对应list集合
        roleService.removeByIds(ids);
        return Result.ok(null);
    }
}
